//
//  AppointmentViewController.m
//  Service4U
//
//  Created by Hai Nguyen on 10/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AppointmentViewController.h"
#import "Service4UAppDelegate.h"

@implementation AppointmentViewController

@synthesize scrollView;
@synthesize titleBarLabel;
@synthesize proposedDateLabel;
@synthesize proposedTimeLabel;
@synthesize yourRequireLabel;
@synthesize genderLabel;
@synthesize firstNameLabel;
@synthesize lastNameLabel;
@synthesize phoneLabel;
@synthesize emailLabel;
@synthesize femaleLabel;
@synthesize maleLabel;
@synthesize dateLabel;
@synthesize hourTimeLabel;
@synthesize optionRequestLabel;
@synthesize optionEmailLabel;
@synthesize reqireFirstNameLabel;
@synthesize reqireLastNameLabel;
@synthesize reqirePhoneLabel;
@synthesize yourRequestTextView;
@synthesize firstNameField;
@synthesize lastNameField;
@synthesize phoneField;
@synthesize emailField;
@synthesize femaleCheckbox;
@synthesize maleCheckbox;
@synthesize submitButton;
@synthesize resetButton;
@synthesize cancelButton;
@synthesize proposedDateButton;
@synthesize proposedTimeButton;
@synthesize hideKeyboardButton;
@synthesize inputTextViewView;
@synthesize inputTextViewBakground;
@synthesize inputTextViewButton;
@synthesize pickerView;
@synthesize timePicker;
@synthesize pickerDoneBackground;
@synthesize pickerDoneButton;
@synthesize isProposedDate;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self.view addSubview:scrollView];
	[scrollView setFrame:CGRectMake(0, 44, 320, 367)];
	[scrollView setContentSize:CGSizeMake(320, 540)];
	scrollView.scrollEnabled = YES;
	[self setCurrentDateTime];
	yourRequestTextView.inputAccessoryView = inputTextViewView;
	[self.view addSubview:pickerView];
	[pickerView setFrame:CGRectMake(0, 411, 320, 411)];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	self.scrollView = nil;
	[pickerView release];
	[timePicker release];
	[scrollView release];
	[titleBarLabel release];
	[proposedDateLabel release];
	[proposedTimeLabel release];
	[yourRequireLabel release];
	[genderLabel release];
	[firstNameLabel release];
	[lastNameLabel release];
	[phoneLabel release];
	[emailLabel release];
	[femaleLabel release];
	[maleLabel release];
	[dateLabel release];
	[hourTimeLabel release];
	[optionRequestLabel release];
	[optionEmailLabel release];
	[reqireFirstNameLabel release];
	[reqireLastNameLabel release];
	[reqirePhoneLabel release];
	[yourRequestTextView release];
	[firstNameField release];
	[lastNameField release];
	[phoneField release];
	[emailField release];
	[femaleCheckbox release];
	[maleCheckbox release];
	[submitButton release];
	[resetButton release];
	[cancelButton release];
	[proposedDateButton release];
	[proposedTimeButton release];
	[hideKeyboardButton release];
	[inputTextViewView release];
	[inputTextViewBakground release];
	[inputTextViewButton release];
	[pickerDoneBackground release];
	[pickerDoneButton release];
    [super dealloc];
}

#pragma mark -
#pragma mark UITextField delegate method

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
	if ([textField isEqual:firstNameField]) {
		[scrollView scrollRectToVisible:CGRectMake(0, 50, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	}
	if ([textField isEqual:lastNameField]) {
		[scrollView scrollRectToVisible:CGRectMake(0, 100, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	}
	if ([textField isEqual:phoneField]) {
		[scrollView scrollRectToVisible:CGRectMake(0,120, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	}
	if ([textField isEqual:emailField]) {
		[scrollView scrollRectToVisible:CGRectMake(0,160, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	}
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
	if ([textField isEqual:firstNameField]) {
		[lastNameField becomeFirstResponder];
	}
	if ([textField isEqual:lastNameField]) {
		[phoneField becomeFirstResponder];
	}
	
	if ([textField isEqual:phoneField]) {
		[emailField becomeFirstResponder];
	}
	if ([textField isEqual:emailField]) {
		[textField resignFirstResponder];
		[scrollView scrollRectToVisible:CGRectMake(0, 50, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	}	
	return NO;
}


#pragma mark -
#pragma mark UITextView delegate method

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
	[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
	return YES;
}

#pragma mark -
#pragma mark Custom Method

-(IBAction) checkboxGenderPressed:(id)sender{
	if ([sender isEqual:femaleCheckbox]) {
		[maleCheckbox setSelected:NO];
		[femaleCheckbox setSelected:YES];
	}
	if ([sender isEqual:maleCheckbox]) {
		[maleCheckbox setSelected:YES];
		[femaleCheckbox setSelected:NO];
	}
	[self hideKeyboardButtonPressed:nil];
}

- (IBAction) hideKeyboardButtonPressed:(id)sender{
	[firstNameField resignFirstResponder];
	[lastNameField resignFirstResponder];
	[phoneField resignFirstResponder];
	[emailField resignFirstResponder];
	[yourRequestTextView resignFirstResponder];
	[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}

- (IBAction) doneButtonPressed{
	[firstNameField becomeFirstResponder];
}

- (IBAction) resetButtonPressed{
	firstNameField.text = @"";
	lastNameField.text = @"";
	phoneField.text = @"";
	emailField.text = @"";
	yourRequestTextView.text = @"";
}


- (IBAction) submitButtonPressed{
	if ([firstNameField.text length] == 0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"First name is required field!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		[reqireFirstNameLabel setTextColor:[UIColor redColor]];
		return;
	}
	if ([lastNameField.text length] == 0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Last name is required field!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		[reqireLastNameLabel setTextColor:[UIColor redColor]];
		return;
	}
	if ([phoneField.text length] == 0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Phone is required field!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		[reqirePhoneLabel setTextColor:[UIColor redColor]];
		return;
	}
	MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
	picker.messageComposeDelegate = self;
	
	picker.recipients = [NSArray arrayWithObject:OFFICE_TELEPHONE];
    NSString *gender = femaleCheckbox.selected?@"Female":@"Male";
	picker.body = [NSString stringWithFormat:@"%@ %@,\n %@, %@,%@,%@,\n %@ %@",firstNameField.text, lastNameField.text,gender
                   ,phoneField.text, emailField.text, yourRequestTextView.text,dateLabel.text, hourTimeLabel.text];
	
	[mainAppDelegate.rootViewController presentModalViewController:picker animated:YES];
	[picker release];
}

#pragma mark -
#pragma mark MFMessageComposeViewControllerDelegate Method

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
	NSString *message;
	if (result == MessageComposeResultSent) {
		message = @"Your message has been sent.";
		[self setCurrentDateTime];
	} else if (result == MessageComposeResultFailed) {
		message = @"Your message sent failed";
	} else {
		message = @"Sending has been Cancel";
	}
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:message delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
	[alert show];
	[alert release];
	[mainAppDelegate.rootViewController dismissModalViewControllerAnimated:YES];
}

- (IBAction) showPickerView:(id) sender{
	[self hideKeyboardButtonPressed:nil];
	if ([sender isEqual:proposedDateButton]) {
		[timePicker setDatePickerMode:UIDatePickerModeDate];
	} else {
		[timePicker setDatePickerMode:UIDatePickerModeTime];
	}
	[UIView beginAnimations:@"ShowPickerView" context:nil];
	[UIView setAnimationDuration:0.5f];
	CGRect frame = [pickerView frame];
	frame.origin.y -= 411;
	pickerView.frame = frame;
	[UIView commitAnimations];
	if ([sender isEqual:proposedDateButton]) {
		isProposedDate = YES;
	}
	else {
		isProposedDate = NO;
	}
}

- (IBAction) hidePickerView:(id) sender{
	[UIView beginAnimations:@"ShowPickerView" context:nil];
	[UIView setAnimationDuration:0.5f];
	CGRect frame = [pickerView frame];
	frame.origin.y += 411;
	pickerView.frame = frame;
	[UIView commitAnimations];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	if (isProposedDate == YES) {
		[dateFormatter setDateStyle:NSDateFormatterLongStyle];
		NSString *stringDate = [dateFormatter stringFromDate:timePicker.date];
		dateLabel.text = stringDate;
	}
	else {
		[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
		NSString *stringTime = [dateFormatter stringFromDate:timePicker.date];
		hourTimeLabel.text = stringTime;
	}
	[dateFormatter release];
	[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}

- (void) setCurrentDateTime{
	NSDate *currentDate = [NSDate date];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateStyle:NSDateFormatterLongStyle];
	NSString *stringDate = [dateFormatter stringFromDate:currentDate];
	dateLabel.text = stringDate;
	[dateFormatter release];
	dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
	NSString *timetring = [dateFormatter stringFromDate:currentDate];
	NSLog(@"time string %@",timetring);
	hourTimeLabel.text = timetring;
	[dateFormatter release];	
}
@end
